/**
 * 
 */
package ch.comtools.servermanager.common.resource;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Display;

/**
 * @author Roger Dudler <roger.dudler@gmail.com>
 * @version $Id: ResourceManager.java 47 2007-04-18 06:45:30Z roger.dudler $
 */
public class ResourceManager {

	private Map<RGB, Color> colors = new HashMap<RGB, Color>();
	private Map<Object[], Font> fonts = new HashMap<Object[], Font>();
	private static ResourceManager instance;
	
	/**
	 * 
	 */
	private ResourceManager() {
		
	}
	
	/**
	 * @return
	 */
	public static ResourceManager getInstance() {
		if (instance == null) {
			instance = new ResourceManager();
		}
		return instance;
	}
	
	/**
	 * Get a color.
	 * @param r
	 * @param g
	 * @param b
	 * @return
	 */
	public Color getColor(int r, int g, int b) {
		RGB rgb = new RGB(r, g, b);
		Color color = null;
		if (!this.colors.containsKey(rgb)) {
			this.colors.put(rgb, new Color(Display.getCurrent(), r, g, b));
		}
		color = this.colors.get(rgb);
		return color;
	}
	
	/**
	 * Get a font.
	 * @param string
	 * @param i
	 * @param normal
	 * @return
	 */
	public Font getFont(String string, int i, int normal) {
		Object[] fontData = new Object[] { string, i, normal };
		if (!this.fonts.containsKey(fontData)) {
			this.fonts.put(fontData, new Font(Display.getCurrent(), string, i, normal));
		}
		return this.fonts.get(fontData);
	}
	
	/**
	 * Dispose all resources.
	 */
	public void dispose() {
		// dispose colors
		for (Color color : this.colors.values()) {
			color.dispose();
		}
		// dispose fonts
		for (Font font : this.fonts.values()) {
			font.dispose();
		}
	}
	
}
